package testOne2Ten;

import java.util.Stack;

/**
 * 从尾到头打印链表
 * Created by Wongy on 2018/3/13.
 */
public class Test5 {
    private static void printByStack(ListNode linkNode) {
        if (linkNode == null) {
            System.out.println("Test5.printByStack , 10 : linkNode = " + null);
        } else {
            Stack<Integer> stack = new Stack<>();
            while (linkNode != null) {
                stack.push(linkNode.value);
                linkNode = linkNode.next;
            }
            while (!stack.isEmpty()) {
                System.out.print(stack.pop() + " ");
            }
            System.out.println();
        }
    }

    private static void printByRec(ListNode linkNode) {
        if (linkNode != null) {
            if (linkNode.next != null) {
                printByRec(linkNode.next);
            }
            System.out.print(linkNode.value + " ");
        }
    }

    public static void main(String args[]) {
        ListNode listNode1 = new ListNode(1);
        ListNode listNode2 = new ListNode(2);
        ListNode listNode3 = new ListNode(3);
        ListNode listNode4 = new ListNode(4);
        ListNode listNode5 = new ListNode(5);
        listNode1.next = listNode2;
        listNode2.next = listNode3;
        listNode3.next = listNode4;
        listNode4.next = listNode5;
        printByStack(listNode1);
        printByRec(listNode1);

    }
}

class ListNode {
    int value;
    ListNode next;

    public ListNode(int value) {
        this.value = value;
    }
}
